#include "connectionPool.hpp"
#include "groupmodel.hpp"

/**
 * @brief 创建群组
 *
 * @param group
 */
void GroupModel::createGroup(Group &group)
{
    char sql[1024] = {0};
    sprintf(sql, "insert into allgroup(groupname,groupdesc) values('%s','%s')", group.getName().c_str(), group.getDesc().c_str());

    shared_ptr<MySQL> mysql = ConnectionPool::getConnectionPool()->getMySQL();
    mysql->update(sql);
    group.setId(mysql_insert_id(mysql->getConnection()));
}
/**
 * @brief 加入群组
 *
 * @param userid
 * @param groupid
 * @param role
 */
void GroupModel::addGroup(int userid, int groupid, string role)
{
    char sql[1024] = {0};
    sprintf(sql, "insert into groupuser values(%d,%d,'%s')", groupid, userid, role.c_str());

    shared_ptr<MySQL> mysql = ConnectionPool::getConnectionPool()->getMySQL();
    mysql->update(sql);
}
/**
 * @brief 用户的群组查询
 *
 * @param userid
 * @return vector<Group>
 */
vector<Group> GroupModel::queryGroup(int userid)
{
    char sql[1024] = {0};
    sprintf(sql, "select a.id,a.groupname,a.groupdesc from allgroup a join groupuser g on a.id=g.groupid where g.userid=%d", userid);

    shared_ptr<MySQL> mysql = ConnectionPool::getConnectionPool()->getMySQL();
    vector<Group> ret;
    MYSQL_RES *res = mysql->query(sql);
    if (res != nullptr)
    {
        MYSQL_ROW row;
        // 群组信息
        while ((row = mysql_fetch_row(res)) != nullptr)
        {
            Group group;
            group.setId(atoi(row[0]));
            group.setName(row[1]);
            group.setDesc(row[2]);
            ret.push_back(group);
        }
        mysql_free_result(res);
    }
    // 群组用户信息
    for (Group &group : ret)
    {
        sprintf(sql, "select u.id,u.name,u.state,g.grouprole from user u join groupuser g on g.userid=u.id where g.groupid=%d", group.getId());
        res = mysql->query(sql);
        if (res != nullptr)
        {
            MYSQL_ROW row;
            while ((row = mysql_fetch_row(res)) != nullptr)
            {
                GroupUser user;
                user.setId(atoi(row[0]));
                user.setName(row[1]);
                user.setState(row[2]);
                user.setRole(row[3]);
                group.getUsers().push_back(user);
            }
            mysql_free_result(res);
        }
    }
    return ret;
}
/**
 * @brief 指定群组的用户id查询
 *
 * @param userid
 * @param groupid
 * @return vector<int> 通过返回用户id集合，便于userid进行群发消息
 */
vector<int> GroupModel::queryGroupUsers(int userid, int groupid)
{
    char sql[1024] = {0};
    sprintf(sql, "select userid from groupuser where groupid=%d and userid!=%d", groupid, userid);

    shared_ptr<MySQL> mysql = ConnectionPool::getConnectionPool()->getMySQL();
    vector<int> ret;
    MYSQL_RES *res = mysql->query(sql);
    if (res != nullptr)
    {
        MYSQL_ROW row;
        while ((row = mysql_fetch_row(res)) != nullptr)
        {
            ret.push_back(atoi(row[0]));
        }
        mysql_free_result(res);
        // while (!mysql_next_result(mysql->getConnection())) // 清除缓存
        // {
        //     res = mysql_store_result(mysql->getConnection());
        //     mysql_free_result(res);
        // }
    }
    return ret;
}